import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:guji/global.dart';
import 'package:guji/manager/main_manager.dart';
import 'package:provider/provider.dart';

class EditPage extends StatefulWidget {
  EditPage({Key key, this.id, this.data}) : super(key: key);
  final int id;
  final data;
  @override
  State<StatefulWidget> createState() => _EditPageState();
}

class _EditPageState extends State<EditPage> {
  String _title = '';
  String _secretText = '';
  TextEditingController _titleController = TextEditingController();
  TextEditingController _contentController = TextEditingController();
  @override
  void initState() {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
    if (widget.id != null) {
      _titleController.text = widget.data['title'];
      _contentController.text = widget.data['data'];
      setState(() {
        _title = widget.data['title'];
        _secretText = widget.data['data'];
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(color: Global.primaryColor),
        backgroundColor: Global.backgroundColor,
        toolbarHeight: 50,
        elevation: 0,
        actions: [
          InkWell(
            onTap: () {
              if (widget.id != null) {
                _mainManager.update(widget.id.toString(), _title, _secretText);
                Navigator.of(context).pop();
              } else {
                String id = DateTime.now().millisecondsSinceEpoch.toString();
                _mainManager.insert(id, _title, _secretText);
                Navigator.of(context).pop();
              }
            },
            child: Center(
              child: Text(
                '保存   ',
                style: TextStyle(
                  color: Global.fontSecondColor,
                  fontSize: 18,
                  fontFamily: 'FangZhengShuSongFanTi',
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
          ),
        ],
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(color: Global.backgroundColor),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                margin: EdgeInsets.fromLTRB(15, 20, 15, 0),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Global.fontSecondColor.withOpacity(.06),
                ),
                child: ConstrainedBox(
                  constraints: BoxConstraints(
                    maxWidth: MediaQuery.of(context).size.width,
                  ),
                  child: TextField(
                    onTap: () {},
                    maxLines: 1,
                    controller: _titleController,
                    style: TextStyle(
                      color: Global.fontColor,
                      fontFamily: 'FangZhengShuSongFanTi',
                      fontSize: 20,
                      fontWeight: FontWeight.w600,
                      height: 2,
                    ),
                    decoration: InputDecoration.collapsed(
                      hintText: '简记标题',
                      hintStyle: TextStyle(
                        color: Global.fontSecondColor.withOpacity(0.5),
                        fontFamily: 'FangZhengShuSongFanTi',
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                    onChanged: (text) {
                      setState(() {
                        _title = text;
                      });
                    },
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.fromLTRB(15, 20, 15, 20),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Global.fontSecondColor.withOpacity(.06),
                ),
                child: ConstrainedBox(
                  constraints: BoxConstraints(
                    maxWidth: MediaQuery.of(context).size.width,
                  ),
                  child: TextField(
                    onTap: () {},
                    maxLines: 20,
                    controller: _contentController,
                    style: TextStyle(
                      color: Global.fontColor,
                      fontFamily: 'FangZhengShuSongFanTi',
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      height: 2,
                    ),
                    decoration: InputDecoration.collapsed(
                      hintText: '简记内容',
                      hintStyle: TextStyle(
                        color: Global.fontSecondColor.withOpacity(0.5),
                        fontFamily: 'FangZhengShuSongFanTi',
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                    onChanged: (text) {
                      setState(() {
                        _secretText = text;
                      });
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
